<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增活动管理')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: summernote-css" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-activity-add">
<!--            <div class="form-group">
                <label class="col-sm-3 control-label is-required">活动类型：</label>
                <div class="col-sm-8">
                    <select name="type" class="form-control m-b" required>
                        <option value="">所有</option>
                    </select>
                    <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 代码生成请选择字典属性</span>
                </div>
            </div>-->
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">标题：</label>
                <div class="col-sm-8">
                    <textarea name="title" class="form-control" required></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">内容：</label>
                <div class="col-sm-8">
                    <textarea class="summernote" id="content" name="content" required></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">图片：</label>
                <div class="col-sm-8">
                    <input id="img" type="hidden" name="img">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="file" name="file" type="file" accept=".jpg, .png, .jpeg">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">活动开始时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="startTime"  class="form-control"  id="datetimepicker-demo-3" placeholder="yyyy-MM-dd HH:mm:ss" type="text" required>
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">活动结束时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="endTime"  class="form-control"  id="datetimepicker-demo-3" placeholder="yyyy-MM-dd HH:mm:ss" type="text" required>
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: summernote-js" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <script th:inline="javascript">
        var prefix = ctx + "dl/activity"
        $("#form-activity-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-activity-add').serialize());
            }
        }

        $("input[name='startTime']").datetimepicker({
            format: "yyyy-mm-dd hh:ii:ss",
            autoclose: true
        });

        $("input[name='endTime']").datetimepicker({
            format: "yyyy-mm-dd hh:ii:ss",
            autoclose: true
        });

        $("#file").fileinput({
            uploadUrl: prefix + "/upload", //上传的地址
            showPreview: true, //是否显示预览
            maxFileCount: 1, //表示允许同时上传的最大文件个数
            enctype: 'multipart/form-data',
            maxFileSize: 0,
            allowedPreviewTypes: ['image'],
            allowedFileTypes: ['image'],
            allowedFileExtensions : ['jpg', 'png', 'jpeg'],
            previewFileIcon: "<i class='glyphicon glyphicon-king'></i>"
        }).on('fileuploaded', function (event, data, previewId, index) {
            //同步上传成功回调，如果是异步上传成功会回调fileuploaded，用错的话，回调是不会执行的
            //通过 data.response.Json对象属性 获得返回数据
            if (data.response.code == 0) {
                $("#img").val(data.response.data)
                console.log(data.response.data)
            } else {
                $.modal.alertWarning(data.response.msg);
            }
        });



        $(function() {
            $('.summernote').summernote({
                lang: 'zh-CN',
                callbacks: {
                    onChange: function(contents, $edittable) {
                        $("input[name='" + this.id + "']").val(contents);
                    },
                    onImageUpload: function(files) {
                        var obj = this;
                    	var data = new FormData();
                    	data.append("file", files[0]);
                    	$.ajax({
                            type: "post",
                            url: ctx + "common/upload",
                    		data: data,
                    		cache: false,
                    		contentType: false,
                    		processData: false,
                    		dataType: 'json',
                    		success: function(result) {
                    		    if (result.code == web_status.SUCCESS) {
                    		        $('#' + obj.id).summernote('insertImage', result.url);
                    		    } else {
                    		        $.modal.alertError(result.msg);
                    		    }
                    		},
                    		error: function(error) {
                    		    $.modal.alertWarning("图片上传失败。");
                    		}
                    	});
                    }
                }
            });
        });
    </script>
</body>
</html>